home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / vcommon / subs.c < prev    next >
C/C++ Source or Header  |  1997-09-09  |  10KB  |  346 lines

  1. #include "project.h"
  2. #ifdef PROJECT_VOPTS
  3.    #include "vopts.h"
  4. #endif
  5. #ifdef PROJECT_VMAKE
  6.    #include "vmake.h"
  7. #endif
  8.  
  9. Prototype void free_mem(void *mem, int size);
  10. Prototype char *savestr(char *str);
  11. Prototype void *get_mem(int size);
  12. Prototype int request(int nochoice, int txtidx, char *parm, char *option);
  13. Prototype void set_busy(void);
  14. Prototype void set_idle(void);
  15. Prototype void cover_window(void);
  16. Prototype void uncover_window(void);
  17.  
  18. /***********************************************************************************
  19.  * Procedure: free_mem
  20.  * Synopsis:  free_mem(mem, size);
  21.  * Purpose:   Return allocated memory to the global pool for reuse.
  22.  ***********************************************************************************/
  23. void free_mem(void *mem,
  24.               int size
  25.              )
  26. {
  27.    free(mem);
  28. }
  29.  
  30. /***********************************************************************************
  31.  * Procedure: get_mem
  32.  * Synopsis:  mem = get_mem(size);
  33.  * Purpose:   Allocate zeroed memory for use.  If the memory can not be allocated,
  34.  *            an error is indicated in the title bar and NULL is returned.
  35.  ***********************************************************************************/
  36. void *get_mem(int size
  37.             )
  38. {
  39.    void *rslt;
  40.  
  41.    rslt = malloc(size);
  42.    if (!rslt)
  43.    {
  44.       sprintf(global.title, "No Memory for %d bytes\n", size);
  45.       if (global.window)
  46.       {
  47.          SetWindowTitles(global.window, global.title, NULL);
  48.       }
  49.    }
  50.    else
  51.    {
  52.       memset(rslt, 0, size);
  53.    }
  54.    return(rslt);
  55. }
  56.  
  57. /***********************************************************************************
  58.  * Procedure: savestr
  59.  * Synopsis:  copystr = savestr(str);
  60.  * Purpose:   Save a copy of a string for later reference
  61.  ***********************************************************************************/
  62. char *savestr(char *str
  63.              )
  64. {
  65.    char *p;
  66.  
  67.    if (!*str) return("");
  68.    p = get_mem(strlen(str)+1);
  69.    strcpy(p, str);
  70.    return(p);
  71. }
  72.  
  73. /***********************************************************************************
  74.  * Procedure: request
  75.  * Synopsis:  rc = request(nochoice, txtidx, parm, option)
  76.  * Purpose:   Present a requester to the user
  77.  ***********************************************************************************/
  78. int request(int nochoice,
  79.             int txtidx,
  80.             char *parm,
  81.             char *option
  82.            )
  83. {
  84.    int rqrc;
  85.  
  86. #ifdef PROJECT_VMAKE
  87. /* VOpts doesn't support REXX - under VMake want to fail with no requester */
  88.    if (global.inrexx)
  89.    {
  90.       global.rexxrc = txtidx; /* rexx RC matches config file text entries  */
  91.       return 0;               /* equivalent to user clicking "Cancel"      */
  92.    }
  93. #endif
  94.  
  95.    if (IntuitionBase->LibNode.lib_Version >= 36)
  96.    {
  97.       int i;
  98.       ULONG iflags;
  99.       char *args[5];
  100.       struct EasyStruct RespES = { sizeof(struct EasyStruct), 0, NULL, "%s", "%s|%s" };
  101.  
  102.       i = 0;
  103.       args[i++] = global.text[txtidx];
  104.       if (parm)
  105.       {
  106.          RespES.es_TextFormat = "%s\n%s";
  107.          args[i++] = parm;
  108.       }
  109.  
  110.       if (nochoice)
  111.       {
  112.          RespES.es_GadgetFormat = global.text[TEXT_OK];
  113.       }
  114.       else
  115.       {
  116.          args[i++] = global.text[TEXT_OK];
  117.          if (option)
  118.          {
  119.             RespES.es_GadgetFormat = "%s|%s|%s";
  120.             args[i++] = option;
  121.          }
  122.          args[i++] = global.text[TEXT_CANCEL];
  123.       }
  124.  
  125.       iflags = IDCMP_DISKINSERTED;
  126.  
  127.       rqrc = EasyRequestArgs(global.window, &RespES, &iflags, (APTR)args);
  128.    }
  129.    else
  130.    {
  131.       struct IntuiText body, body1, pos, neg;
  132.       int width, height;
  133.  
  134.       body.FrontPen  =
  135.       body.BackPen   = -1;
  136.       body.DrawMode  = JAM1;
  137.       body.LeftEdge  = 6;
  138.       body.TopEdge   = 4;
  139.       body.ITextFont = &global.ri.TextAttr;
  140.       body.IText     = global.text[txtidx];
  141.       body.NextText  = NULL;
  142.  
  143.       body1 = body;
  144.       pos   = body;
  145.       neg   = body;
  146.       width = IntuiTextLength(&body);
  147.       height = 4 * global.iheight;
  148.       if (parm)
  149.       {
  150.          int ewidth;
  151.          body.NextText = &body1;
  152.          body1.IText   = parm;
  153.          body1.TopEdge = global.iheight;
  154.          height += global.iheight;
  155.          ewidth = IntuiTextLength(&body1);
  156.          if (ewidth > width) width = ewidth;
  157.       }
  158.       width += 36;  /* margins, resize gadget etc. */
  159.       pos.IText = global.text[TEXT_OK];
  160.       if (nochoice)
  161.       {
  162.          neg.IText = pos.IText;
  163.       }
  164.       else
  165.       {
  166.          neg.IText = global.text[TEXT_CANCEL];
  167.       }
  168.  
  169.       rqrc = AutoRequest(global.window,
  170.                          &body, &pos, &neg, 0, 0, width, height);      
  171.    }
  172.  
  173. #ifdef PROJECT_VMAKE
  174. /* VOpts doesn't support ARexx - want VMake to set ARexx return code */
  175. /* unless user followed a success path from the requester            */
  176.    if ((rqrc == 0) || nochoice)
  177.       global.rexxrc = txtidx;
  178. #endif
  179.  
  180.    return rqrc;
  181. }
  182.  
  183. #ifdef PROJECT_VMAKE
  184. const USHORT busy_data[] = {
  185.  
  186. 0x0000, 0x0000,   /* Position and control words */
  187. /*************************/            /*************************/
  188. /*                       */            /*                       */
  189. /*  .... .*.. .... ....  */  0x0400,   /*  .... .*** **.. ....  */  0x07C0,
  190. /*  .... .... .... ....  */  0x0000,   /*  .... .*** **.. ....  */  0x07C0,
  191. /*  .... ...* .... ....  */  0x0100,   /*  .... ..** *... ....  */  0x0380,
  192. /*  .... .... .... ....  */  0x0000,   /*  .... .*** ***. ....  */  0x07E0,
  193. /*  .... .*** **.. ....  */  0x07C0,   /*  ...* **** **** *...  */  0x1FF8,
  194. /*  ...* **** **** ....  */  0x1FF0,   /*  ..** **** ***. **..  */  0x3FEC,
  195. /*  ..** **** **** *...  */  0x3FF8,   /*  .*** **** **.* ***.  */  0x7FDE,
  196. /*  ..** **** **** *...  */  0x3FF8,   /*  .*** **** *.** ***.  */  0x7FBE,
  197. /*  .*** **** **** **..  */  0x7FFC,   /*  **** **** .*** ****  */  0xFF7F,
  198. /*  .*** ***. **** **..  */  0x7EFC,   /*  **** **** **** ****  */  0xFFFF,
  199. /*  .*** **** **** **..  */  0x7FFC,   /*  **** **** **** ****  */  0xFFFF,
  200. /*  ..** **** **** *...  */  0x3FF8,   /*  .*** **** **** ***.  */  0x7FFE,
  201. /*  ..** **** **** *...  */  0x3FF8,   /*  .*** **** **** ***.  */  0x7FFE,
  202. /*  ...* **** **** ....  */  0x1FF0,   /*  ..** **** **** **..  */  0x3FFC,
  203. /*  .... .*** **.. ....  */  0x07C0,   /*  ...* **** **** *...  */  0x1FF8,
  204. /*  .... .... .... ....  */  0x0000,   /*  .... .*** ***. ....  */  0x07E0,
  205. /*                       */            /*                       */
  206. /*************************/            /*************************/
  207. 0x0000, 0x0000   /* Reserved for the system */
  208. };
  209.  
  210. #define BUSY_WIDTH   16
  211. #define BUSY_HEIGHT  16
  212. #define BUSY_XOFFSET -6
  213. #define BUSY_YOFFSET  0
  214. static struct Gadget *cover;
  215. static int cover_count;
  216. USHORT *busy_pointer;
  217.  
  218. /***********************************************************************************
  219.  * Procedure: set_busy
  220.  * Synopsis:  (void)set_busy();
  221.  * Purpose:   Mark the window as busy so no input will come into it
  222.  ***********************************************************************************/
  223. void set_busy(void)
  224. {
  225. #ifdef WA_BusyPointer
  226.    if (IntuitionBase->LibNode.lib_Version >= 39)
  227.    {
  228.       struct TagItem taglist[3];
  229.  
  230.       taglist[0].ti_Tag  = WA_BusyPointer;
  231.       taglist[0].ti_Data = TRUE;
  232.  
  233.       taglist[1].ti_Tag  = WA_PointerDelay;
  234.       taglist[1].ti_Data = TRUE;
  235.  
  236.       taglist[2].ti_Tag  = TAG_DONE;
  237.       taglist[2].ti_Data = TRUE;
  238.  
  239.       /* Put up the busy pointer, with pointer-delay */
  240.       SetWindowPointer( global.window, taglist);
  241.    }
  242.    else
  243. #endif
  244.    {
  245.       if (busy_pointer == NULL)
  246.       {
  247.          busy_pointer = AllocMem(sizeof(busy_data), MEMF_CHIP);
  248.          if (busy_pointer)
  249.          {
  250.             memcpy(busy_pointer, busy_data, sizeof(busy_data));
  251.             SetPointer(global.window, busy_pointer,
  252.                  BUSY_HEIGHT, BUSY_WIDTH, BUSY_XOFFSET, BUSY_YOFFSET);
  253.          }
  254.       }
  255.    }
  256.    /* We need to create a single gadget that covers the entire screen so that */
  257.    /* They can not click on any of the gadgets.                               */
  258.    cover_window();
  259. }
  260.  
  261. /***********************************************************************************
  262.  * Procedure: set_idle
  263.  * Synopsis:  (void)set_idle();
  264.  * Purpose:   Remove the busy mark on the window
  265.  ***********************************************************************************/
  266. void set_idle(void)
  267. {
  268.  
  269.    uncover_window();
  270. #ifdef WA_BusyPointer
  271.    if (IntuitionBase->LibNode.lib_Version >= 39)
  272.    {
  273.       struct TagItem taglist[1];
  274.       taglist[0].ti_Tag  = TAG_DONE;
  275.       taglist[0].ti_Data = TRUE;
  276.  
  277.       /* Put up the busy pointer, with pointer-delay */
  278.       SetWindowPointer( global.window, taglist);
  279.    }
  280.    else
  281. #endif
  282.    {
  283.       /* Do the 1.3 stuff here */
  284.    }
  285.    if (busy_pointer)
  286.    {
  287.       ClearPointer(global.window);
  288.       FreeMem(busy_pointer, sizeof(busy_data));
  289.    }
  290.  
  291.    busy_pointer = NULL;
  292. }
  293.  
  294. /***********************************************************************************
  295.  * Procedure: cover_window
  296.  * Synopsis:  (void)cover_window();
  297.  * Purpose:   cover the window with a bool gadget - deactivate other gadgets
  298.  ***********************************************************************************/
  299. void cover_window(void)
  300. {
  301.    struct Gadget *gad;
  302.  
  303.    if (cover == 0) /* is there a cover already? */
  304.    {
  305.       gad = (struct Gadget *)get_mem(sizeof(struct Gadget));
  306.       if (gad)
  307.       {
  308.          cover = gad;
  309.    
  310.          gad->LeftEdge    = global.ri.WindowLeft;
  311.          gad->TopEdge     = global.ri.WindowTitle;
  312.          gad->Width       = global.width;
  313.          gad->Height      = global.height - global.ri.WindowTitle;
  314.          gad->Flags       = GADGHNONE;
  315.          gad->Activation  = 0;
  316.          gad->GadgetType  = BOOLGADGET;
  317.    
  318.          AddGadget(global.window, gad, 0);
  319.       }
  320.       cover_count = 0;
  321.    }
  322.    else /* one cover is enough - as long as we don't take it off too soon */
  323.       cover_count++;
  324. }
  325.  
  326. /***********************************************************************************
  327.  * Procedure: uncover_window
  328.  * Synopsis:  un(void)cover_window();
  329.  * Purpose:   remove bool gadget from window - reactivate other gadgets
  330.  ***********************************************************************************/
  331. void uncover_window(void)
  332. {
  333.    if (cover_count == 0) /* is this the last request to remove it? */
  334.    {
  335.       if (cover)
  336.       {
  337.          RemoveGadget(global.window, cover);
  338.          free_mem(cover, sizeof(struct Gadget));
  339.          cover = NULL;
  340.       }
  341.    }
  342.    else /* don't take it off till the fat lady sings */
  343.       cover_count--;
  344. }
  345. #endif
  346.